from django.shortcuts import render,get_object_or_404
from django.http import  HttpResponse,HttpResponseRedirect
from .models import  Quesion, Choice, Item, ItemCount
from django.template import loader
from django.urls import  reverse
from django.views import  generic
from .test import *
from .control import get_items_info
import datetime
import  pytz
from useradmin.models import users

# Create your views here.
tz = pytz.timezone('Asia/Shanghai')

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        return Quesion.objects.order_by('-pub_date')






class DetailView(generic.DetailView):
    model = Quesion
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Quesion
    template_name = 'polls/results.html'


def index(request):
    try:
        user = request.COOKIES['username']
    except KeyError:
        return render(request, 'useradmin/Login.html')

    latest_question_list = Quesion.objects.order_by('pub_date')
    item_list = Item.objects.all()
    contex = {'latest_question_list':latest_question_list,
              'item_list':item_list,
              'user':user,
              'options':[1,2,3,4,5]}

    return  render(request,'polls/index.html',contex)

def vote(request,question_id):
    print("vote!")
    question = get_object_or_404(Quesion,pk=question_id)
    try:

        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except(KeyError,Choice.DoesNotExist):
        return render(request,'polls/detail.html',{'quesion':question,
                                                   'error_message':"select nothing",})
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return  HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

def choose_num(request,item_id):
    number = -1
    #item = get_object_or_404(Item,pk=item_id)

    number = request.POST['Number']
    itemcount = ItemCount.create(number,item_id)
    print(datetime.datetime.now(tz))
    itemcount.save()
    dct = get_items_info(item_id)
    print(dct)
    return render(request,'polls/myres.html',context=dct)






def detail_count(request,item_id):
    item = get_object_or_404(Item,pk=item_id)
    mycontext = {'item':item,'options':range(0,6)}
    return render(request,'polls/detailv2.html',context=mycontext)






def detail(request, question_id):
    question = get_object_or_404(Quesion, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

def detailV2(request, item_id):
    print('detailV2:' + item_id)
    item = get_object_or_404(Item, pk=item_id)
    return render(request, 'polls/detailv2.html', {'item': item})



def results(request, question_id):
    question = get_object_or_404(Quesion, pk=question_id)
    print(question)
    return render(request,'polls/results.html',{'question':question})



def about(request):
    return  render(request,'polls/about.html')


def custome_first_step(request):
    strnum = request.POST['Number']
    num = int(strnum)
    context = {'nums':range(0,num),'options':range(-3,4)}
    return render(request, 'polls/custome1.html', context)

def custome_second_step(request):
    dick = {}
    list=[]
    queStr='SSXinvincible1:'
    addStr='SSXinvincible2:'
    total = int(request.POST['total'])
    for i in range(0,total):
        dick[request.POST['a'+str(i)]]=request.POST['affect'+str(i)]
        list.append(queStr + request.POST['a'+str(i)] + ";"+ addStr + request.POST['affect'+str(i)])


    context = {'list':list,'dick':dick}

    return render(request,'polls/custome2.html',context=context)

def custome_third_step(request):

    dick = request.POST['dick']
    dic = eval(dick)
    user = users.objects.filter(username=request.COOKIES['username'])
    u = user.first()
    dic['u#'] = u.id
    dic['q#'] = '?'
    print(dic)
    postman.deliver(str(dic))
    return HttpResponse("ok")

